﻿using System;
using System.Collections.Generic;
using SimpleFS.Core;
using SimpleFS.Services.Transactions.Implementations;
using SimpleFS.Services.Transactions.Implementations.Directories;
using SimpleFS.Core.Implementations;

namespace SimpleFS.Services.Transactions
{
    public class Directory : AbstractDirectory
    {
        internal IResourceManager resourceManager = null;

        public Directory(IDirectory internalObject, IResourceManager resourceManager) : base(internalObject)
        {
            this.resourceManager = resourceManager;
        }

        #region IDirectory Members

        public override IDirectory[] Directories
        {
            get
            {
                IDirectory[] directories = base.internalObject.Directories;
                Directory[] newDirectories = new Directory[directories.Length];

                for (int index = 0; index < directories.Length; index++)
                {
                    newDirectories[index] = new Directory(
                        directories[index], this.resourceManager);
                }

                return newDirectories;
            }
        }

        public override IFile[] Files
        {
            get
            {
                IFile[] files = base.internalObject.Files;
                File[] newFiles = new File[files.Length];

                for (int index = 0; index < files.Length; index++)
                {
                    newFiles[index] = new File(
                        files[index], this.resourceManager);
                }

                return newFiles;
            }
        }

        public override IFile AddFile(string fileName)
        {
            return new File(
                base.internalObject.AddFile(fileName),
                this.resourceManager);
        }

        public override IDirectory AddDirectory(string directoryName)
        {
            return new Directory(
                base.internalObject.AddDirectory(directoryName),
                this.resourceManager);
        }

        #endregion

        #region IFileSystemObject Members

        public override void Create()
        {
            Create create = new Create(base.internalObject, this.resourceManager);
            create.Execute();
        }

        public override void Copy(IFileSystemObject targetD)
        {
            Directory target = targetD as Directory;

            if (!target.Exists())
            {
                target.Create();
            }

            foreach (Directory subDirectory in this.Directories)
            {
                Directory subTargetDirectory = target.AddDirectory(subDirectory.Name) as Directory;
                subDirectory.Copy(subTargetDirectory);
            }

            foreach (File file in this.Files)
            {
                File targetFile = target.AddFile(file.Name) as File;
                file.Copy(targetFile);
            }
        }

        public override void Move(IFileSystemObject target)
        {
            this.Copy(target);
            this.Delete();
        }

        public override void Delete()
        {
            SimpleFS.Services.Transactions.Implementations.Delete delete
                = new Delete(base.internalObject, this.resourceManager);
            delete.Execute();
        }

        public override void Rename(string newName)
        {
            SimpleFS.Services.Transactions.Implementations.Rename rename
                = new Rename(this.internalObject, newName, this.resourceManager);
            rename.Execute();
        }

        #endregion
    }
}
